package student.aistar.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import student.aistar.mapper.UserMapper;
import student.aistar.model.dto.AddressDTO;
import student.aistar.model.entity.Address;
import student.aistar.model.entity.Addressdetail;
import student.aistar.model.entity.User;
import student.aistar.service.IUserService;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public void Login(User user) {
        userMapper.InsertUser(user);
    }

    @Override
    public User loginIndex(User user) {
        return userMapper.selectUser(user);
    }

    @Override
    public AddressDTO findAddressById(Integer userId) {
        List<Address> addressList = userMapper.selectAddressById(userId);
        List<Addressdetail> addressdetailList = new ArrayList<>();

        for (Address address : addressList) {
            addressdetailList.add(userMapper.selectAddressDetail(address.getAddressId()));
        }

//        System.out.println(addressdetailList);
//        System.out.println(addressList);

        AddressDTO addressDTO = new AddressDTO();
        addressDTO.setAddressDetailList(addressdetailList);
        addressDTO.setAddressList(addressList);
        return addressDTO;
    }

    @Override
    public void addAddress(Address address) {
        userMapper.insertAddress(address);
    }


    /**
     * 查询用户点击确认的地址
     * @param addressId
     * @return
     */
    @Transactional
    @Override
    public Address getCheckAddress(Integer addressId){
        return userMapper.selectAddressByAddressId(addressId);
    }

    @Override
    public void reviseAddress(Address address) {
        userMapper.updateAddressByAddressId(address);
    }
}
